<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-3-1下午8:05:12
 *
 */
 
 /**
  * CanGoo Comment library
  * @package blog
  * @subpackage library
  * @category library front
  * @author mervin.wong
  * @link http://mervin.me/project/cangoo description
  */
 
 class Comment{
 	/*
 	 * 超级对象引用
 	 */
 	private $_CI;
 	/**
 	 * 一级评论内容的ID
 	 */
 	private $_parent_id = array();
 	private $_children_id = array();
 	/**
 	 * 一条评论的内容信息
 	 */
 	private $_comment_info;
 	private $_user_data = array();
 	
 	private $_current_cid = 0;
 	
 	public $current_pid = 0;
 	public function __construct(){
 		$this->_CI = & get_instance();
 		$this->_CI->load->helper('url');
 		$this->_CI->load->helper('date');
 		$this->_CI->load->model('blog/comment_model');
 	}
 	public function __get($name){
 		return $this->$name;
 	}
 	public function __set($name, $value){
 		$this->$name = $value;
 	}
 	
 	/**
 	 * 是否存在该评论
 	 */
 	public function have(){
 		if($this->_CI->comment_model->is_have(array('cid'),'comment', array('pid'=>$this->current_pid))){
 			$ids = $this->_CI->comment_model->get_all(array('cid'), 'comment', array('pid'=>$this->current_pid, 'parent'=>0), array('addTime'=>'DESC'));
 			foreach($ids as $id){
 				array_push($this->_parent_id, $id['cid']);
 			}
 			return true;
 		}else{
 			return false;
 		}
 	}
 	
 	/**
 	 * 获取下一条评论
 	 */
 	public function next(){
 		$this->_current_cid = current($this->_parent_id);
 		if($this->_current_cid){
 			next($this->_parent_id);
 			
 			$field = array('cid', 'pid', 'text', 'userData', 'addTime');
 			$this->_comment_info = $this->_CI->comment_model->get_one($field, 'comment', array('cid'=>$this->_current_cid));
 			$this->_user_data = unserialize($this->_comment_info->userData);
 			return true;
 		}else{
			return false; 			
 		}
 	}
 	public function has_children(){
 		if($this->_CI->comment_model->is_have(array('cid'),'comment', array('parent'=>$this->_current_cid))){
			$this->_get_children_id();
 			return true;
 		}else{
 			return false;
 		}
 	}
 	/**
 	 * 获取下一条评论
 	 */
 	 public function children(){
 	 	$this->_current_cid = current($this->_children_id);
 		if($this->_current_cid){
 			next($this->_children_id);
 			
 			$field = array('cid', 'pid', 'text', 'userData', 'addTime');
 			$this->_comment_info = $this->_CI->comment_model->get_one($field, 'comment', array('cid'=>$this->_current_cid));
 			$this->_user_data = unserialize($this->_comment_info->userData);
 			return true;
 		}else{
			return false; 			
 		} 	 	
 	 }
 	 
 	 /**
 	  * 用户昵称
 	  */
 	 public function author(){
 	 	$data = $this->_user_data;
 	 	return $data['name'];
 	 }
 	 /**
 	  * 留言者邮箱
 	  */
 	 public function email(){
 	 	$data = $this->_user_data;
 	 	return $data['email']; 	 	
 	 }
 	 /**
 	  * 留言者网址
 	  */
 	  public function url(){
 	 	$data = $this->_user_data;
 	 	return $data['url']; 	  	
 	  }
 	  /**
 	   * 留言时间
 	   */
 	   public function date(){
 	   		return mdate('%Y-%m-%d %H:%i:%s', $this->_comment_info->addTime);
 	   }
 	   /**
 	    * 留言内容 
 	    */
 	   public function text(){
 	   		return $this->_comment_info->text;
 	   }
 	   /**
 	    * 留言内容的ID
 	    */
 	    public function cid(){
 	    	return $this->_comment_info->cid;
 	    }
 	 
 	 /**
 	  * 评论
 	  */
 	 public function reply(){
 	 	$values['name'] = 'mervin';
 	 	$values['email'] = 'mervin0502@163.com';
 	 	$values['url'] = 'http://mervin.me';
 	 	$values['text'] = '这是一个默认留言';
 	 	$values['cid'] = 0;
 	 	$values['parent'] = 0;
 	 	$values['pid'] = $this->current_pid;
 	 	//return $this->_form(site_url('article/comment_add'), $values);
 	 	return $this->_form('', $values);
 	 }
 	 /**
 	  * 留言表格
 	  * @access private
 	  * @param string $action form action value
 	  * @param array $values field value
 	  * @return string form 
 	  */
 	 private function _form($action, $values){
 	 	$this->_CI->load->helper('form');
 	 	$form = form_open($action,array('name'=>'reply_form', 'id'=>'reply_form', 'class'=>'reply_form'));
 	 	$form .= '<p>';
 	 	$form .= form_label('昵称:', 'reply_name', array('class'=>'reply_label'));
 	 	$form .= form_input(array('name'=>'reply_name', 'id'=>'reply_name', 'class'=>'reply_name', 'size'=>14, 'value'=>$values['name']));
 	 	$form .= '</p><p>';
 	 	$form .= form_label('邮箱:', 'reply_email', array('class'=>'reply_label'));
 	 	$form .= form_input(array('name'=>'reply_email', 'id'=>'reply_email', 'class'=>'reply_email', 'size'=>14, 'value'=>$values['email']));
 	 	$form .= '</p><p>';
 	 	$form .= form_label('网址:', 'reply_url', array('class'=>'reply_label'));
 	 	$form .= form_input(array('name'=>'reply_url', 'id'=>'reply_url', 'class'=>'reply_url', 'size'=>14, 'value'=>$values['url']));
 	 	$form .= '</p><p>';
 	 	$form .= form_textarea(array('name'=>'reply_text', 'id'=>'reply_text', 'class'=>'reply_text', 'width'=>'100%','height'=>'60px', 'value'=>$values['text']));
 	 	$form .= '</p><p>';
 	 	$form .= form_hidden('reply_cid', $values['cid']);
 	 	$form .= form_hidden('reply_parent', $values['parent']);
 	 	$form .= form_hidden('reply_pid', $values['pid']);
 	 	$form .= form_hidden('current_url', urlencode(current_url().'#comments'));
 	 	$form .= form_hidden('is_submit', 'ok');
 	 	$form .= form_reset('reply_reset', '重写', 'class="reply_button"');
 	 	$form .= form_submit('reply_submit', '提交', 'class="reply_button"');
 	 	$form .= '</p>';
 	 	$form .= form_close();
 	 	return $form;
 	 }
 	 
 	 public function _get_children_id($parent_id = 0){
		if($parent_id == 0){
			$parent_id = $this->_current_cid;
		}
		$children = $this->_CI->comment_model->get_all(array('cid'), 'comment', array('parent'=>$parent_id, 'pid'=>$this->current_pid), array('addTime'=>'ASC'));
		if(count($children) >0 ){
			foreach($children as $v){
				array_push($this->_children_id, $v['cid']);
				$this->_get_children_id($v['cid']);				
			}
		}
 	 }
 }
 
/*End of file Comment.php*/
/* Location : ./apps/blog/libraries/Comment.php */
